
// src/components/EnvSelect/index.tsx
import React, { useEffect, useState } from 'react';
import { ProFormSelect } from '@ant-design/pro-components';
import { get_map } from '@/services/system_api';



const SystemDictSelect: React.FC<SystemSelectProps> = ({
  name = 'name',
  label = 'label',
  placeholder = '',
  width,
  rules = [{ required: true, message: '' }],
  disabled = false,
  allowClear = true,
  initialValue=null,
  dict_key='',
  onChange,
}) => {
  const [options, setOptions] = useState<ServerOption[]>([]);
  const [loading, setLoading] = useState(false);

  const loadOptions = async () => {
    setLoading(true);
    try {
      const response = await get_map(dict_key);
      if (response.code === 0) {
        setOptions( response.data );
      }
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadOptions();
  }, []);

  return (
    <ProFormSelect
      name={name}
      label={label}
      placeholder={placeholder}
      options={options}
      rules={rules}
      disabled={disabled}
      initialValue={initialValue}
      allowClear={allowClear}
      fieldProps={{
        loading,
        onChange,
      }}
      width={width}
    />
  );
};

export default SystemDictSelect;